/*******************************************************************************
 *
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.action;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.internal.P4TeamProvider;
import net.sourceforge.perforce.ui.internal.Policy;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;
import org.eclipse.ui.actions.WorkspaceModifyOperation;

/**
  * @version  $Revision: 77 $
  * @author   <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class UnmanageAction extends PerforceAction 
{
	// ------------------------------------------------------------------------------ Public Methods

	/** @see IActionDelegate#run(org.eclipse.jface.action.IAction) */
	public void run(IAction action) 
	{
		run(new WorkspaceModifyOperation()
		{
			protected void execute(IProgressMonitor monitor)
				throws InvocationTargetException			 
			{
				try 
				{
					Map map = getProviderMapping();
					Set keySet = map.keySet();
					
					monitor.beginTask("", keySet.size() * 1000);
					monitor.setTaskName("Disconnecting...");
					
					for (Iterator it = keySet.iterator(); it.hasNext();)
					{
						IProgressMonitor subMonitor = Policy.subMonitorFor(monitor, 1000);
						
						P4TeamProvider provider = (P4TeamProvider) it.next();
						List list = (List) map.get(provider);
						IResource[] providerResources = 
							(IResource[]) list.toArray(new IResource[list.size()]);
						for (int i = 0; i < providerResources.length; i++) 
						{
							IResource resource = providerResources[i]; 
							RepositoryProvider.unmap((IProject) resource);
						}
					}
				}
				catch (TeamException e)
				{
					throw new InvocationTargetException(e);
				}
				finally
				{
					monitor.done();
				}
			}

		}, PROGRESS_DIALOG);
	}
	
	// --------------------------------------------------------------------------- Protected Methods

	/** @see PerforceAction#isEnabled() */
	protected boolean isEnabled() 
		throws PerforceException 
	{
		IResource[] resources = getSelectedResources();
		if (resources.length == 0) 
		{
			return false;
		}
		
		for (int i = 0; i < resources.length; i++) 
		{
			if (resources[i].getType() != IResource.PROJECT) 
			{
				return false;
			}
			
			RepositoryProvider provider = 
				RepositoryProvider.getProvider(
					resources[i].getProject(), 
					PerforceCorePlugin.getTypeId());
			if (provider == null) 
			{
				return false;
			}
		}
		return true;
	}
}
